home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / tcqbsnip.zip / SUBVARS.BAS < prev    next >
BASIC Source File  |  1997-06-20  |  2KB  |  58 lines

  1. ' SUBVARS.BAS
  2. ' by Tika Carr
  3. '
  4. ' Donated to the public domain
  5. ' No warranties or guarantees are expressed or implied.
  6. '
  7. ' Purpose: A demonstration of local and global variables between subroutines
  8.  
  9. DECLARE SUB Test ()
  10. DECLARE SUB Test2 ()
  11. DIM SHARED GlobalVar$   'This is a global variable, you can use it in
  12.                         'any SUB or FUNCTION and it won't lose its
  13.                         'value.
  14.  
  15. LocalVar$ = "World"     'This is a local variable. This will not hold
  16.                         'its value in any SUB or FUNCTION.
  17.  
  18. GlobalVar$ = "Hello"    'We can give a global variable a value
  19.                         'anytime we want, even in a SUB or FUNCTION and
  20.                         'it will stay that way until we change it
  21.                         'again.
  22.  
  23. PRINT "Here are the variables before we go to a SUB: "
  24. PRINT "GlobalVar$ ="; GlobalVar$
  25. PRINT "LocalVar$ ="; LocalVar$
  26. PRINT
  27. PRINT "Here we will now go to the SUB to print them out:"
  28. CALL Test
  29. PRINT
  30. PRINT "Here they are again, after the sub: "
  31. PRINT "GlobalVar$ ="; GlobalVar$
  32. PRINT "LocalVar$ ="; LocalVar$
  33. PRINT
  34. PRINT "Now, we will change them in another sub and print them out: "
  35. CALL Test2
  36. PRINT
  37. PRINT "Here we are back in the main program again: "
  38. PRINT "GlobalVar$ ="; GlobalVar$   'This one held the new value
  39. PRINT "LocalVar$ ="; LocalVar$     'This one didn't hold the new
  40.                                    'value.
  41. PRINT : PRINT "All Done!"
  42. END
  43.  
  44. SUB Test
  45. PRINT "GlobalVar$ ="; GlobalVar$    'This one will hold its value
  46. PRINT "LocalVar$ ="; LocalVar$      'This one won't print anything
  47. END SUB
  48.  
  49. SUB Test2
  50. GlobalVar$ = "Another"
  51. LocalVar$ = "String"
  52. PRINT "GlobalVar$ ="; GlobalVar$    'This one is now changed
  53. PRINT "LocalVar$ ="; LocalVar$      'So is this, but won't print in
  54.                                     'main program because its local to
  55.                                     'the SUB.
  56. END SUB
  57.  
  58.